﻿using Admin.Application.Const;
using Admin.Application.DynamicApi.System.User.Dto;
using Admin.Database.Model;
using Framework.Core;
using Framework.DI;
using Framework.DynamicApiController;
using Framework.SqlSugar.Repository;
using SqlSugar.IOC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.Application.DynamicApi.System.User;

/// <summary>
/// 系统用户角色服务
/// </summary>
[Service]
public class SysUserRoleService : ISelfTransientAutoInject
{
	private readonly CacheHelp _cacheHelp;
	private readonly SqlSugarRepository<SysUserRole> _sysUserRoleRep;

	public SysUserRoleService(CacheHelp cacheHelp,
		SqlSugarRepository<SysUserRole> sysUserRoleRep)
	{
		_cacheHelp = cacheHelp;
		_sysUserRoleRep = sysUserRoleRep;
	}

	/// <summary>
	/// 授权用户角色
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	public async Task GrantUserRole(UserRoleInput input)
	{
		await _sysUserRoleRep.DeleteAsync(u => u.UserId == input.UserId);

		if (input.RoleIdList == null || input.RoleIdList.Count < 1) return;
		var roles = input.RoleIdList.Select(u => new SysUserRole
		{
			UserId = input.UserId,
			RoleId = u
		}).ToList();
		await _sysUserRoleRep.InsertRangeAsync(roles);
		_cacheHelp.Remove(CacheConst.KeyUserButton + input.UserId);
	}

	/// <summary>
	/// 根据角色Id删除用户角色
	/// </summary>
	/// <param name="roleId"></param>
	/// <returns></returns>
	public async Task DeleteUserRoleByRoleId(long roleId)
	{
		await _sysUserRoleRep.AsQueryable()
			 .Where(u => u.RoleId == roleId)
			 .Select(u => u.UserId)
			 .ForEachAsync(userId =>
			 {
				 _cacheHelp.Remove(CacheConst.KeyUserButton + userId);
			 });

		await _sysUserRoleRep.DeleteAsync(u => u.RoleId == roleId);
	}

	/// <summary>
	/// 根据用户Id删除用户角色
	/// </summary>
	/// <param name="userId"></param>
	/// <returns></returns>
	public async Task DeleteUserRoleByUserId(long userId)
	{
		await _sysUserRoleRep.DeleteAsync(u => u.UserId == userId);
		_cacheHelp.Remove(CacheConst.KeyUserButton + userId);
	}

	/// <summary>
	/// 根据用户Id获取角色集合
	/// </summary>
	/// <param name="userId"></param>
	/// <returns></returns>
	public async Task<List<SysRole>> GetUserRoleList(long userId)
	{
		var sysUserRoleList = await _sysUserRoleRep.AsQueryable()
			.Includes(u => u.SysRole)
			.Where(u => u.UserId == userId).ToListAsync();
		return sysUserRoleList.Where(u => u.SysRole != null).Select(u => u.SysRole).ToList();
	}

	/// <summary>
	/// 根据用户Id获取角色Id集合
	/// </summary>
	/// <param name="userId"></param>
	/// <returns></returns>
	public async Task<List<long>> GetUserRoleIdList(long userId)
	{
		return await _sysUserRoleRep.AsQueryable()
			.Where(u => u.UserId == userId).Select(u => u.RoleId).ToListAsync();
	}

	/// <summary>
	/// 根据角色Id获取用户Id集合
	/// </summary>
	/// <param name="roleId"></param>
	/// <returns></returns>
	public async Task<List<long>> GetUserIdList(long roleId)
	{
		return await _sysUserRoleRep.AsQueryable()
			.Where(u => u.RoleId == roleId).Select(u => u.UserId).ToListAsync();
	}
}